//
//  PanView.m
//  HuiTu
//
//  Created by macpro on 16/9/12.
//  Copyright © 2016年 macpro. All rights reserved.
//

#import "PanView.h"
const float space = 0.005;
const float dotLineLength = 200;

@interface PanView ()
{
    int radios;
    int lineW;
    CGPoint centerPoint;
    int timerNum;
    CAShapeLayer *layer;
    NSString *currentProgress;//当前进度
}
// 每个动画时长的数组
@property (nonatomic, strong) NSMutableArray *arrayDruing;
@property (nonatomic, strong) UILabel *labelTitle;
@property (nonatomic, strong) NSMutableArray *arrayFirsDotLine;
@property (nonatomic, strong) NSMutableArray *arraySecondDotLine;
@property (nonatomic, strong) NSMutableArray *arrayThirdDotLine;
@property (nonatomic, strong) NSMutableArray *arrauFourDorLine;
@end

@implementation PanView

-(void)dealloc
{
    NSLog(@"%s",__func__);
}
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self initData];
        [self addObserver:self forKeyPath:@"progress" options:NSKeyValueObservingOptionNew context:nil];
    }
    return self;
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    
    NSString *number  = change[@"new"];
    currentProgress = number;
    [self setNeedsDisplay];
}
// 初始化绘图要用到的数据
- (void)initData {
    self.arraycolor = @[[UIColor redColor], [UIColor yellowColor], [UIColor greenColor], [UIColor blueColor]].mutableCopy;
    self.arrayScale = @[@0.1, @0.2, @0.3, @0.4].mutableCopy;
    self.sumAnimationDruing = 2;
    self.num = self.arraycolor.count;
    lineW = 100;
    timerNum = 0;
    radios = lineW / 2.0;
    float center = MIN(self.frame.size.width / 2, self.frame.size.height / 2);
    centerPoint = CGPointMake(center, center);
}
// 绘制覆盖层
- (void)drawFirstRound {
    [layer removeFromSuperlayer];
    layer = [CAShapeLayer layer];
    layer.strokeColor = [[UIColor whiteColor] CGColor];
    layer.strokeStart = 0;
    layer.strokeEnd = 1;
    layer.fillColor = [UIColor clearColor].CGColor;
    layer.lineWidth = lineW;
    [self.layer addSublayer:layer];
    
    CGFloat endAngle = - M_PI_2  +  2 * M_PI * [currentProgress floatValue];
    if (endAngle >=  M_PI_2 * 3) {
        return;
    }
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:centerPoint radius:radios startAngle: endAngle endAngle: M_PI_2 * 3 clockwise:YES];
    layer.path = path.CGPath;
}
//绘图
- (void)drawRect:(CGRect)rect {
    // Drawing code
    [self drawFirstRound];
}



@end
